package com.leha.core.services.imp;

import com.google.common.net.InetAddresses;
import com.leha.core.dao.LhBackendUserMapper;
import com.leha.core.entity.mybatis.LhBackendUser;
import com.leha.core.entity.mybatis.LhBackendUserExample;
import com.leha.core.services.AdminServices;
import com.leha.core.services.ErrorMessageServices;
import com.leha.utils.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class AdminServicesImpl implements AdminServices {
    @Autowired
    LhBackendUserMapper backendUserMapper;
    @Autowired
    ErrorMessageServices errorMessageServices;

    @Override
    public LhBackendUser validate(String username, String password) {
        String codePwd = PasswordUtils.encodePassword(username, password);
        LhBackendUserExample ex = new LhBackendUserExample();
        ex.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(codePwd).andStatusEqualTo(0);
        List<LhBackendUser> res = backendUserMapper.selectByExample(ex);
        if(res.size()>0) {
            return res.get(0);
        } else {
            return null;
        }
    }

    @Override
    public LhBackendUser AddUser(String username, String password, String group) {
        String codePwd = PasswordUtils.encodePassword(username, password);
        LhBackendUser u = new LhBackendUser();
        u.setUsername(username);
        u.setPassword(codePwd);
        u.setUserGroup(group);
        backendUserMapper.insertSelective(u);
        return u;
    }
}
